package com.iweb.controller;

import cn.hutool.core.date.DateUtil;
import com.iweb.pojo.Book;
import com.iweb.pojo.Place;
import com.iweb.service.BookService;
import com.iweb.service.PlaceService;
import com.iweb.service.UserService;
import com.iweb.utils.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;
    /*@Autowired
    PlaceService placeService;*/

    /*@RequestMapping(value = "/insertBook")
    public String insertBook(@RequestParam Map map, HttpServletRequest request,Place place) {
        Book book = new Book();
       map.put("bookTime",DateUtil.now()) ;
        DataUtils.Map2Bean(book, map);
        //把userID绑定到book,而且还要更新placeStatus
        book.setUserID(Integer.parseInt(request.getSession().getAttribute("userID").toString()));
        System.out.println(book);
        bookService.insertBook(book,place);
        return "success";
    }*/

    @RequestMapping(value = "/insertBook")
    public String insertBook(@RequestParam Map map, HttpServletRequest request, Place place) {
        Book book = new Book();
        String timeStr = (String) map.get("time");
        String[] times = timeStr.split("-");
        map.put("startTime", map.get("bookDate") + " " + times[0]);
        map.put("endTime", map.get("bookDate") + " " + times[1]);
        map.put("bookTime", DateUtil.now());
        DataUtils.Map2Bean(book, map);
        //把userID绑定到book,而且还要更新placeStatus
        book.setUserID(Integer.parseInt(request.getSession().getAttribute("userID").toString()));
        System.out.println(book);
        bookService.insertBook(book, place);
        return "success";
    }

    @RequestMapping(value = "/updateBook")
    public String updateBook(@RequestParam Map map, HttpServletRequest request) {
        Book book = new Book();
        String timeStr = (String) map.get("time");
        String[] times = timeStr.split("-");
        map.put("startTime", map.get("bookDate") + " " + times[0]);
        map.put("endTime", map.get("bookDate") + " " + times[1]);
        DataUtils.Map2Bean(book, map);
        //把userID绑定到book,而且还要更新placeStatus
        book.setUserID(Integer.parseInt(request.getSession().getAttribute("userID").toString()));
        System.out.println(book);
        if(bookService.updateBook(book))
            return "success";
        return  null;
    }

    @RequestMapping(value = "/getBook", method = RequestMethod.GET)
    public ModelAndView selectBoByStartTime(HttpServletResponse response, @RequestParam Map map) throws IOException {
        Book book = new Book();
        DataUtils.Map2Bean(book, map);
        List<Map> books= bookService.selectBookByStartTime(book);
        ModelAndView mvc=new ModelAndView("listBook");
        mvc.addObject("books",books);
       return mvc;
    }

    @RequestMapping(value = "queryMyBook",method = RequestMethod.GET)
    public String queryMyBook( HttpServletRequest req){
        int userID =Integer.parseInt(req.getSession().getAttribute("userID").toString()) ;
        System.out.println(userID);
        List books = bookService.selectBookByUserID(userID);
        System.out.println(books);
        req.setAttribute("books",books);
        return "user/usercenter/myBook";
    }

    @RequestMapping(value = "checkBook")
    @ResponseBody
    public String checkBookByDate(@RequestParam Map map){
        if("".equals(map.get("bookDate"))){
            return "预约日期不存在";
        }
        Book book = new Book();
        String timeStr = (String) map.get("time");
        String[] times = timeStr.split("-");
        map.put("startTime", map.get("bookDate") + " " + times[0]);
        map.put("endTime", map.get("bookDate") + " " + times[1]);
        map.put("bookTime", DateUtil.now());
        DataUtils.Map2Bean(book, map);

        if(bookService.checkBookByDate(book))
            return "success";
        return "fail";
    }

    @RequestMapping(value = "/deleteBook")
    @ResponseBody
    public String deleteBook(@RequestParam Map map, HttpServletRequest request,Place place) {
        Book book = new Book();
        DataUtils.Map2Bean(book, map);
        //把userID绑定到book,而且还要更新placeStatus
        book.setUserID(Integer.parseInt(request.getSession().getAttribute("userID").toString()));
        System.out.println(book);
        if (bookService.deleteBook(book,place))
            return "success";
        return "error";
    }


}
